package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.DeClanTable;
import com.lineage.server.datatables.lock.ClanEmblemReading;
import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.serverpackets.S_Emblem;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.DeClan;
import com.lineage.server.templates.L1EmblemIcon;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_EmblemDownload extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_EmblemDownload.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport()) {
        int emblemid = readD();
        L1EmblemIcon emblemIcon = ClanEmblemReading.get().getEmblem(emblemid);
        L1Clan clan = ClanReading.get().getTemplate(emblemIcon.get_clanid());
        if (clan != null && emblemIcon != null)
          pc.sendPackets((ServerBasePacket)new S_Emblem(emblemIcon)); 
        L1EmblemIcon emblemIcon2 = ClanEmblemReading.get().getEmblem(emblemid);
        DeClan deClan = DeClanTable.get().get(emblemIcon2.get_clanid());
        if (deClan != null && emblemIcon2 != null) {
          pc.sendPackets((ServerBasePacket)new S_Emblem(emblemIcon2));
          return;
        } 
        return;
      } 
      return;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
